package rev4;

import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;

import org.junit.Test;

import rev3.MyString;
import rev3.NodeMyString;

public class Rev4TestGets {

	/**
	 * First look up the entries put into the map upon creation of the DHT bootstrap class
	 */
	@Test
	public void test2()
	{
		NodeMyString remoteNode = null;
		try {
            Registry registry = LocateRegistry.getRegistry();
            
            String name = "n1";
            // Get the remote ref
            remoteNode = (NodeMyString) registry.lookup(name);
            System.out.println("found node "+name);

            MyString retMyStr = remoteNode.get("1::davey");
            assertTrue("jones".equalsIgnoreCase(retMyStr.getMyString()) );
                     
            retMyStr = remoteNode.get("93::dr");
            assertTrue("zhivago".equalsIgnoreCase(retMyStr.getMyString()) );
            
            retMyStr = remoteNode.get("8::abraham");
            assertNotNull(retMyStr);
            assertTrue("lincoln".equalsIgnoreCase(retMyStr.getMyString()) );
            
            retMyStr = remoteNode.get("115::barack");
            assertTrue("obama".equalsIgnoreCase(retMyStr.getMyString()) );
            
            retMyStr = remoteNode.get("1234::does_not_exist");
            assertNull(retMyStr);
            
            name = "n30";
            // Get the remote ref
            remoteNode = (NodeMyString) registry.lookup(name);
            System.out.println("found node "+name);

            retMyStr = remoteNode.get("89::mike");
            assertTrue("michalak".equalsIgnoreCase(retMyStr.getMyString()) );
                        
            // Verify bad hash key fails
            try {
				retMyStr = remoteNode.get("16::");
				fail();
			} catch (RuntimeException e) {	}
			 try {
					retMyStr = remoteNode.get("::missing_nodekey");
					fail();
			} catch (Exception e) {	}
			
        } catch (Exception e) {
            System.err.println("NodeMyString exception:");
            e.printStackTrace();
            fail();
        }

	}

}
